package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with Intellij IDEA
 * Description
 * User:ASUS
 * Date:2024-08-04
 * Time:23:22
 */
public class AddOperation implements IOPeration{
        public void work(BookList bookList){
            System.out.println("新增图书……");
            int currentSize=bookList.getUsedSize();
            if(currentSize==bookList.getBooks().length){
                System.out.println("书架满了，不能放了");
                return;
            }
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入书名：   ");
            String name=scanner.nextLine();
            System.out.println("请输入作者：   ");
            String author=scanner.nextLine();
            System.out.println("请输入书的类型：   ");
            String type=scanner.nextLine();
            System.out.println("请输入价格：   ");
            int price=scanner.nextInt();

            Book newbook=new Book(name,author,price,type);
            for (int i=0;i<currentSize;i++){
                Book book=bookList.getBooks(i);
                if(book.getName().equals(name)){
                    System.out.println("有这本书，不能增加");
                    System.out.println(book);
                    return;
                }
            }
            bookList.setBooks(currentSize,newbook);
            bookList.setUsedSize(currentSize+1);
            System.out.println("新增图书成功");
        }
}
